// ----------------------------------
// RSDK Project: Sonic 1/Sonic 2
// Script Description: Push Button Object
// Script Author: Christan Whitehead/Simon Thomley
// Unpacked by Rubberduckycooly's script unpacker
// ----------------------------------

//-------Aliases-------//
private alias 48 : TYPE_PUSHBUTTON

// Function declarations
reserve function PushButton_DebugDraw
reserve function PushButton_DebugSpawn

// Static Values

// Tables

function PushButton_DebugDraw
	DrawSprite(0)
end function


function PushButton_DebugSpawn
	CreateTempObject(TypeName[Push Button], 0, object.xpos, object.ypos)
end function


event ObjectMain
	object.value0 = 0
	foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
		if object.frame == 0
			BoxCollisionTest(C_BOX, object.entityPos, -14, -4, 14, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
			if checkResult == 1
				object.value0 = 1
				object[currentPlayer].ypos += 0x60000
				PlaySfx(SfxName[Button Press], 0)
			end if
		else
			if object[currentPlayer].yvel >= 0
				BoxCollisionTest(C_PLATFORM, object.entityPos, -14, -4, 14, 12, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
				if checkResult == 1
					object.value0 = 1
					object[currentPlayer].ypos += 0x20000
				else
					BoxCollisionTest(C_TOUCH, object.entityPos, -20, -12, 20, 8, currentPlayer, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO, HITBOX_AUTO)
					if checkResult == 1
						object[currentPlayer].ypos = object[currentPlayer].collisionBottom
						FlipSign(object[currentPlayer].ypos)
						object[currentPlayer].ypos <<= 16
						object[currentPlayer].ypos += object.ypos
						object[currentPlayer].ypos -= 0x20000
						object[currentPlayer].gravity = GRAVITY_AIR
					end if
				end if
			end if
		end if
	next
	if object.propertyValue == 1
		arrayPos0 = object.entityPos
		arrayPos0++
		if object.frame == 0
			BoxCollisionTest(C_BOX, object.entityPos, -14, -4, 14, 8, arrayPos0, -22, -24, 16, 32)
			if checkResult == 1
				object.value0 = 1
				object[arrayPos0].ypos += 0x60000
				object[arrayPos0].xvel = 0
				object[arrayPos0].yvel = 0
				object[arrayPos0].state++
				PlaySfx(SfxName[Button Press], 0)
			end if
		else
			BoxCollisionTest(C_TOUCH, object.entityPos, -14, -4, 14, 8, arrayPos0, -22, -24, 16, 32)
			if checkResult == 1
				object.value0 = 1
			end if
		end if
		if object.value0 == 1
			arrayPos0++
			if object[arrayPos0].state == 0
				object.value1 = stage.deathBoundary
				object.value1 += 0x100000
				stage.deathBoundary = 0x7FFF0000
				arrayPos1 = arrayPos0
				arrayPos1 += 7
				temp0 = 0
				while arrayPos1 >= arrayPos0
					object[arrayPos1].state = 1
					object[arrayPos1].value0 = temp0
					temp0 += 20
					arrayPos1--
				loop
			end if
		end if
		if object.state == 1
			options.touchControls = 0
			object.value2 += 4
			music.volume -= 2
			SetScreenFade(0, 0, 0, object.value2)
			if object.value2 == 384
				fadeColor = 0
				object.value2 = 0
				starPostID = 0
				object.direction = FLIP_NONE
				stage.listPos++
				if options.gameMode == 1
					arrayPos1 = options.saveSlot
					arrayPos1 <<= 3
					if stage.player2Enabled == 1
						saveRAM[arrayPos1] = 3
					else
						saveRAM[arrayPos1] = stage.playerListPos
					end if
					arrayPos1++
					saveRAM[arrayPos1] = player.lives
					arrayPos1++
					saveRAM[arrayPos1] = player.score
					arrayPos1++
					saveRAM[arrayPos1] = player.scoreBonus
					arrayPos1++
					saveRAM[arrayPos1] = stage.listPos
					saveRAM[arrayPos1]++
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.emeralds
					arrayPos1++
					saveRAM[arrayPos1] = specialStage.listPos
					WriteSaveRAM()
				end if
				temp0 = engine.trialMode
				if stage.listPos >= stage.listSize
					temp0 = 1
				end if
				if temp0 == 0
					LoadStage()
				else
					stage.activeList = PRESENTATION_STAGE
					stage.listPos = 0
					LoadStage()
				end if
			end if
		else
			if object[0].ypos > object.value1
				object.state = 1
				recAnimation = 0
				temp0 = 0
				foreach (GROUP_PLAYERS, currentPlayer, ACTIVE_ENTITIES)
					temp1 = object[currentPlayer].animation
					temp1 <<= 8
					temp1 |= object[currentPlayer].direction
					temp1 <<= temp0
					recAnimation |= temp1
					temp0 += 16
				next
				object[0].type = TypeName[Blank Object]
			end if
		end if
	end if
end event


event ObjectDraw
	object.frame = object.value0
	DrawSprite(object.frame)
end event


event ObjectStartup
	LoadSpriteSheet("SBZ/Objects.gif")
	SpriteFrame(-16, -8, 32, 16, 92, 1)
	SpriteFrame(-16, -2, 32, 10, 92, 18)
	foreach (TypeName[Push Button], arrayPos0, ALL_ENTITIES)
		if object[arrayPos0].propertyValue == 1
			object[arrayPos0].value1 = 0x7FFF0000
		end if
	next
	SetTableValue(TypeName[Push Button], DebugMode_ObjCount, DebugMode_TypesTable)
	SetTableValue(PushButton_DebugDraw, DebugMode_ObjCount, DebugMode_DrawTable)
	SetTableValue(PushButton_DebugSpawn, DebugMode_ObjCount, DebugMode_SpawnTable)
	DebugMode_ObjCount++
end event

event RSDKDraw
	DrawSprite(0)
end event

event RSDKLoad
	LoadSpriteSheet("Global/Display.gif")
	SpriteFrame(-16, -16, 32, 32, 1, 143)
end event
